import urllib
import json

restaurant_types = ['italian', 'bars']
restaurant_query = "http://query.yahooapis.com/v1/public/yql?q=select%%20*%%20from%%20local.search%%20where%%20zip%%3D'%s'%%20and%%20query%%3D'%s'&format=json&diagnostics=true&callback="
current_index = 0
current_type = None
restaurant_urls = []

def findRestaurantsByLocationAndType(zip, type):
  global current_index, current_type, restaurant_urls
  if current_type != type:
    current_type = type
    current_index = 0
    restaurant_query_url = restaurant_query % (zip, type)
    jsonDataStr = urllib.urlopen(restaurant_query_url).readlines()[0]
    json_dict = json.loads(jsonDataStr)

    restaurant_urls = []
    results_list = json_dict['query']['results']['Result']
    for result in results_list:
      if 'BusinessUrl' in result:
        restaurant_urls.append(str(result['BusinessUrl']))
  else:
    current_index += 1
    current_index %= len(restaurant_urls)
  
  return restaurant_urls[current_index]



